<template>
  <a-card :bordered="false">

    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="12">

          <a-col :md="4" :sm="8">
            <a-form-item label="车牌号">
              <a-input placeholder="请输入车牌号" v-model="queryParam.plateNo"></a-input>
            </a-form-item>
          </a-col>

          <a-col :md="4" :sm="8">
            <a-form-item label="车辆类型">
              <a-select v-model="queryParam.carNature" placeholder="请输入车辆类型">
                <a-select-option :value="''">请选择车辆类型</a-select-option>
                <a-select-option :value="0">临时车</a-select-option>
                <a-select-option :value="1">月租车</a-select-option>
                <a-select-option :value="2">储值车</a-select-option>
                <a-select-option :value="3">军警车</a-select-option>
                <a-select-option :value="4">白名单</a-select-option>
                <a-select-option :value="5">黑名单</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="8">
            <a-form-item label="入场类型">
              <a-select v-model="queryParam.enterType" placeholder="请选择入场类型">
                <a-select-option :value="''">请选择车辆类型</a-select-option>
                <a-select-option :value="0">正常入场</a-select-option>
                <a-select-option :value="1">重复入场</a-select-option>
                <a-select-option :value="2">手动入场</a-select-option>
                <a-select-option :value="3">补录入场</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>          <a-col :md="4" :sm="8">
          <a-form-item label="出场类型">
            <a-select v-model="queryParam.outerType" placeholder="请选择出场类型">
              <a-select-option :value="''">请选择车辆类型</a-select-option>
              <a-select-option :value="1">正常出场</a-select-option>
              <a-select-option :value="2">手动出场</a-select-option>
              <a-select-option :value="3">补录场内</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
          <a-col :md="4" :sm="8">
            <a-form-item  label="入场地点">
              <a-select placeholder="选择入场地点" v-model="queryParam.enterSerialNo" :options="parkErialarmList"></a-select>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="8">
            <a-form-item   label="出场地点">
              <a-select placeholder="选择出场地点"  v-model="queryParam.outerSerialNo" :options="parkOutAarmList"></a-select>
            </a-form-item>
          </a-col>
         <a-col :md="3" :sm="8">
                    <a-form-item label="入场时间" >
                      <a-date-picker   placeholder="开始日期"  @change="onChangeEntStartTime"/>
                      </a-form-item >
                    </a-col>
              <a-col :md="2" :sm="4">
                <a-form-item >
                <a-date-picker placeholder="结束日期" @change="onChangeEntEndTime" />
                </a-form-item >

            </a-col>
          <a-col :md="3" :sm="8">
            <a-form-item label="出场时间" >
              <a-date-picker   placeholder="开始日期"  @change="onChangOutStartTime"/>
            </a-form-item >
          </a-col>
          <a-col :md="2" :sm="4">
            <a-form-item >
              <a-date-picker placeholder="结束日期" @change="onChangeOutEndTime" />
            </a-form-item >

          </a-col>

          <a-col :md="4" :sm="8" >
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>

            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
<!--      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>-->
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :ellipsis=true
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :scroll="{ x: 1300, y: 1000 }"
        :footer="handleFooterShow"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange">

        <span slot="action" slot-scope="text, record">
                <a @click="handleDetails(record)">进出记录</a>
                    <a-divider type="vertical" />
                <a @click="chargeRecord(record)">收费记录</a>
              <a-divider type="vertical" />
                <a @click="handleEdit(record)">详情</a>



        </span>


        <template slot="title" slot-scope="currentPageData">
          <table>
            <tr>
              <td>
                合计
              </td>

              <td>
                总费用： {{sumInfo.chargeTotalFee===undefined||sumInfo.chargeTotalFee===null?0.00:toFix(sumInfo.chargeTotalFee,2,2)}} 元
              </td>
              <td>
                已支付费用：{{sumInfo.chargePaid===undefined||sumInfo.chargePaid===null?0.00:toFix(sumInfo.chargePaid,2,2)}}元
              </td>
              <td>
                优惠金额：{{sumInfo.couponAmt===undefined||sumInfo.couponAmt===null?0.00:toFix(sumInfo.couponAmt,2,2)}}元
              </td>

            </tr>
          </table>
        </template>
      </a-table>
    <parkCarOutEntranceRecord-modal ref="modalForm" @ok="modalFormOk"></parkCarOutEntranceRecord-modal>
  </a-card>
</template>

<script>
  import ParkCarOutEntranceRecordModal from './modules/ParkCarOutEntranceRecordModal'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import {deleteAction, getAction, downFile} from '@/api/manage'
  import { filterObj } from '@/utils/util';

  export default {
    name: "ParkCarOutEntranceRecordList",
    mixins:[JeecgListMixin],
    components: {
      ParkCarOutEntranceRecordModal
    },
    data () {

      return {

        description: '停车记录管理页面',
        // 表头
        columns: [
          {
            title: '序号',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
           },
		   {
            title: '停车场名称',
            align:"center",
            dataIndex: 'parkName',
            width: 150,
           },
		   {
            title: '车牌号',
            align:"center",
            dataIndex: 'plateNo',
            width: 100

           },

		   {
            title: '车辆性质',
            align:"center",
            dataIndex: 'carNature',
            width: 100,
         customRender: function (t) {
           if(t==='0') {
             return "临时车"
           }
           if(t==='1') {
             return "月租车"
           }
           if(t==='2') {
             return "储值车"
           }
           if(t==='3') {
             return "军警车"
           }
           if(t==='4') {
             return "白名单"
           }
           if(t==='5') {
             return "黑名单"
           }
         }
           },
		   {
            title: '总费用',
            align:"center",
            dataIndex: 'chargeTotalFee',
            width: 100,
         customRender: function (t, r, index) {
           return (t / 100).toFixed(2);
         }
           },
		   {
            title: '优惠券金额',
            align:"center",
            dataIndex: 'couponAmt',
            width: 100,
         customRender: function (t, r, index) {
           return (t / 100).toFixed(2);
         }
           },
		   {
            title: '已支付费用',
            align:"center",
            dataIndex: 'chargePaid',
            width: 100,
         customRender: function (t, r, index) {
           return (t / 100).toFixed(2);
         }
           },

		   {
            title: '进场时间',
            align:"center",
            dataIndex: 'carEnterTime',
            width: 120
           },
		   {
            title: '出场时间',
            align:"center",
            dataIndex: 'carOutTime',
            width: 120
           },
		   {
            title: '停车时长',
            align:"center",
            dataIndex: 'chargeDuration',
            width: 130,
         customRender: function (t, r, index) {
           var day = parseInt(t / 60 / 24);
           var hour = parseInt(t / 60 % 24);
           var min = parseInt(t % 60);
           t = "";
           if (day > 0) {
             t = day + "天";
           }
           if (hour > 0) {
             t += hour + "小时";
           }
           if (min > 0) {
             t += parseFloat(min) + "分钟";
           }

           return t == "" ? "0分钟" : t;
         }
           },
		   {
            title: '入场类型',
            align:"center",
            dataIndex: 'enterType',
            width: 100,
         customRender: function (t) {
           if(t===0) {
             return "正常入场"
           }
           if(t===1) {
             return "重复入场"
           }
           if(t===2) {
             return "手动入场"
           }
           if(t===3) {
             return "补录入场"
           }
         }
           },
		   {
            title: '出场类型',
            align:"center",
            dataIndex: 'outerType',
            width: 100,
          customRender: function (t) {
            if(t===1) {
              return "正常出场"
            }
            if(t===2) {
              return "手动出场"
            }
            if(t===3) {
              return "补录场内"
            }
          }
           },
		   {
            title: '入口工作站',
            align:"center",
            dataIndex: 'enterWorkName',
            width: 100
           },
          {
            title: '入口地点',
            align:"center",
            dataIndex: 'entArmName',
            width: 110
          },
          {
            title: '出口地点',
            align:"center",
            dataIndex: 'outArmName',
            width: 110
          },
		   {
            title: '出口工作站',
            align:"center",
            dataIndex: 'outWorkName',
         width: 100
           },

          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            width: 200,
            key: 'action',
            fixed: 'right',
            scopedSlots: { customRender: 'action' },
          }
        ],
        columnFoot:  [
          {
            key: 'rowIndex',
            width: 140,
            align: "center",
            customRender: function (t, r, index) {
              return {
                children:"小计",
                props:{
                  colspan:2
                }
              }
            }
          },
          {
            width: 150,
            dataIndex: 'count',
            align: "center",
            customRender: function (t, r, index) {
              return t+"笔";
            }
          },
          {
            dataIndex: 'nowMoney',
            width: 150,
            align: "center",
            customRender: function (t, r, index) {
              return "总费用: "+(t / 100).toFixed(2);
            }
          },
          {
            dataIndex: 'activeMoney',
            width: 120,
            align: "center",
            customRender: function (t, r, index) {
              return "已支付费用: "+ (t / 100).toFixed(2);
            }
          },
          {
            dataIndex: 'discount',
            width: 120,
            align: "center",
            customRender: function (t, r, index) {
              return "优惠金额: "+ (t / 100).toFixed(2);
            }
          },

          {
            width: 150
          },
          {
            width: 150
          },
          {
            width: 90,
          },
          {
            width: 80,
          },
          {
            width: 80,
          },

        ],
        footerDataSource:[],
		url: {
          list: "/parkcaroutentrancerecord/parkCarOutEntranceRecord/list",
          delete: "/parkcaroutentrancerecord/parkCarOutEntranceRecord/delete",
          deleteBatch: "/parkcaroutentrancerecord/parkCarOutEntranceRecord/deleteBatch",
          exportXlsUrl: "parkcaroutentrancerecord/parkCarOutEntranceRecord/exportXls",
          importExcelUrl: "parkcaroutentrancerecord/parkCarOutEntranceRecord/importExcel",
          parkOutSerialNoUrl:"/parking/parkArmInfo/selectOutArmList",
          parkEntSerialNoUrl:"/parking/parkArmInfo/selectEntArmList",

    },
        sumInfo:{},
        armType:{},
        parkOutAarmList:{},
        parkErialarmList:{},
       queryParam: {
                "entStartTime":"",
                "entEndTime":"",
                "outStartTime":"",
                "outEndTime":"",
                "type":"-1",

              },
              isorter:{
               column: 'createTime',
               order: 'desc',
             },
    }
  },
  computed: {
    importExcelUrl: function(){
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
    }
  },
   created(){
         this.parkOutSerialNo()
         this.keyupEnter()
         this.parkEntSerialNo()
   },
    methods: {

      chargeRecord(record) {
        this.item =record;
        if (this. item.carEnterId === 'undefined'|| this.item.carEnterId ==="" || this. item.carEnterId === null){

          this.$message.error("未查询到收费记录");
          return
        }
        let batchNumber;
        this.$router.push({
          path:"/parkcaroutentrancerecord/ParkOrderInfoDetailsList",
          query:{
             carEnterId : this.item.carEnterId
          }
        })
      },
      handleDetails(record) {
        this.item =record;
        if (this. item.batchNumber === 'undefined'|| this.item.batchNumber ==="" || this. item.batchNumber === null){

          this.$message.error("未查询到关联记录");
          return
        }
        let batchNumber;
        this.$router.push({
          path:"/parkcaroutentrancerecord/ParkCarOutEntranceRecordDetailsList",
          query:{
            batchNumber : this.item.batchNumber
          }
        })
      },
      parkOutSerialNo(){
        let param = {
          armType:1
        }
        getAction(this.url.parkOutSerialNoUrl, param).then(res=>{
          if(res.result) {
            debugger;
            this.parkOutAarmList = [{value:'',label:'全部'}]
            res.result.forEach(work=>{
              this.parkOutAarmList.push({
                key:work.id,
                label:work.armName,
                value:work.serialNo
              })
            })
          }else {
            this.$message.error(res.message)
          }
        })
      },
      parkEntSerialNo(){
        let param = {
          armType:0
        }
        getAction(this.url.parkEntSerialNoUrl, param).then(res=>{
          if(res.result) {
            debugger;
            this.parkErialarmList = [{value:'',label:'全部'}]
            res.result.forEach(work=>{
              this.parkErialarmList.push({
                key:work.id,
                label:work.armName,
                value:work.serialNo
              })
            })
          }else {
            this.$message.error(res.message)
          }
        })
      },


           onChangeEntStartTime(date, dateString) {
              this.queryParam.entStartTime = dateString;
              },

              onChangeEntEndTime(date, dateString) {
                this.queryParam.entEndTime = dateString;
              },

      onChangOutStartTime(date, dateString) {
        this.queryParam.outStartTime = dateString;
      },

      onChangeOutEndTime(date, dateString) {
        this.queryParam.outEndTime = dateString;
      },

        loadData(arg) {
          if(!this.url.list){
            this.$message.error("请设置url.list属性!")
            return
          }
          //加载数据 若传入参数1则加载第一页的内容
          if (arg === 1) {
            this.ipagination.current = 1;
          }
          var params = this.getQueryParams();//查询条件
          this.loading = true;
          getAction(this.url.list, params).then((res) => {
            if (res.success) {
              this.dataSource = res.result.list.records;
              this.sumInfo = res.result.sumInfo;
              if(this.isNull(this.sumInfo)){
                this.sumInfo.chargeTotalFee = 0;
                this.sumInfo.chargePaid = 0;
                this.sumInfo.couponAmt = 0;
                this.sumInfo.refundFee = 0;
                this.sumInfo.deductionFee = 0;
              }else {
                this.sumInfo.chargePaid = parseFloat(this.sumInfo.chargePaid)
                this.sumInfo.couponAmt = parseFloat(this.sumInfo.couponAmt)
                this.sumInfo.refundFee = parseFloat(this.sumInfo.refundFee)
                this.sumInfo.deductionFee = parseFloat(this.sumInfo.deductionFee)
              }
               this.ipagination.total = res.result.list.total;

            }
            if(res.code===510){
              this.$message.warning(res.message)
            }
            this.loading = false;
          })

        },

        searchQuery() {
                let outStartTime = this.queryParam.outStartTime;
                let outEndTime = this.queryParam.outEndTime;
               let entStartTime = this.queryParam.entStartTime;
               let entEndTime = this.queryParam.entEndTime;
               debugger;
             if (entStartTime != "" && entEndTime != "" ) {
                if (entStartTime > entEndTime) {
                 this.$message.error("开始时间不能大于结束时间");
                 return
                }
              }
                if (outStartTime != "" && outEndTime != "" ) {
                  if (outStartTime > outEndTime) {
                    this.$message.error("开始时间不能大于结束时间");
                    return
                  }
                }
          if (entStartTime != "" && entEndTime != "") {
            if (entStartTime > entEndTime) {
              this.$message.error("开始时间不能大于结束时间");
              return
            }
          }
                if(outEndTime=="" && outEndTime != ""){
                   this.$message.error("请选择开始时间");
                    return
                }
          if(entStartTime=="" && entEndTime != ""){
            this.$message.error("请选择开始时间");
            return
          }
                 if(outStartTime !="" && outEndTime == ""){
                   this.$message.error("请选择结束时间");
                    return
                }
          if(entStartTime !="" && entEndTime == ""){
            this.$message.error("请选择结束时间");
            return
          }
                this.loadData(1);
            },

                getQueryParams(){

                var param = Object.assign({}, this.isorter, this.queryParam);
                param.field = this.getQueryField();
                param.pageNo = this.ipagination.current;
                param.pageSize = this.ipagination.pageSize;
                if (this.queryParam.outStartTime != "" && this.queryParam.outEndTime != "" && this.queryParam.outStartTime !=undefined && this.queryParam.outEndTime !=undefined) {

                  param.outStartTime = this.queryParam.outStartTime + " 00:00:00";
                  param.outEndTime = this.queryParam.outEndTime + " 23:59:59";
                }
                if (this.queryParam.entStartTime != "" && this.queryParam.entEndTime != "" && this.queryParam.entStartTime !=undefined && this.queryParam.entEndTime !=undefined) {
                    param.entStartTime = this.queryParam.entStartTime + " 00:00:00";
                    param.entEndTime = this.queryParam.entEndTime + " 23:59:59";
                  }

                // delete param.createTimeRange; // 时间参数不传递后台
                return filterObj(param);
              },

                loadData(arg) {
                  if(!this.url.list){
                    this.$message.error("请设置url.list属性!")
                    return
                  }
                  //加载数据 若传入参数1则加载第一页的内容
                  if (arg === 1) {
                    this.ipagination.current = 1;
                  }
                  var params = this.getQueryParams();//查询条件
                  this.loading = true;
                  getAction(this.url.list, params).then((res) => {
                    if (res.success) {
                      this.dataSource = res.result.list.records;
                      this.sumInfo = res.result.sumInfo;
                       console.log(  this.sumInfo)
                       this.ipagination.total = res.result.list.total;

                      if(this.isNull(this.sumInfo)){
                        this.sumInfo.chargeTotalFee = 0;
                        this.sumInfo.chargePaid = 0;
                        this.sumInfo.couponAmt = 0;
                        this.sumInfo.refundFee = 0;
                        this.sumInfo.deductionFee = 0;
                      }else {
                        this.sumInfo.chargePaid = parseFloat(this.sumInfo.chargePaid)
                        this.sumInfo.couponAmt = parseFloat(this.sumInfo.couponAmt)
                        this.sumInfo.refundFee = parseFloat(this.sumInfo.refundFee)
                        this.sumInfo.deductionFee = parseFloat(this.sumInfo.deductionFee)
                      }
                      this.loading = false;
                      // 小计
                      this.getSubtotal();
                    }
                    if(res.code===510){
                      this.$message.warning(res.message)
                    }
                    this.loading = false;
                  })
                },
      handleFooterShow(data) {
        return (
            <a-table
                rowKey={Math.random}
                bordered
                pagination={false}
                columns={this.columnFoot}
                dataSource={this.footerDataSource}
                showHeader={false}
                className={this.footerName}
                size="small">
            </a-table>
        )
      },
      getSubtotal(){
        // 小计 footerDataSource dataSource
        let nowMoney = 0;
        let activeMoney = 0;
        let discount = 0;
        let refundFee = 0;
        let deductionFee = 0;
        for (let i = 0; i < this.dataSource.length; i++) {
          let d = this.dataSource[i];
          nowMoney += parseInt(this.isNull(d.chargeTotalFee)?0:d.chargeTotalFee);
          activeMoney += parseInt(this.isNull(d.chargePaid)?0:d.chargePaid);
          discount += parseInt(this.isNull(d.couponAmt)?0:d.couponAmt);
          refundFee +=  parseInt(this.isNull(d.refundFee)?0:d.refundFee);
          deductionFee +=  parseInt(this.isNull(d.deductionFee)?0:d.deductionFee);
        }
        this.footerDataSource = [{
          "count":this.isNull(this.dataSource)?0:this.dataSource.length,
          "nowMoney":nowMoney,
          "activeMoney":activeMoney,
          "discount":discount,
          "refundFee":refundFee,
          "deductionFee":deductionFee
        }]

      },
              batchDel: function () {

              if(!this.url.deleteBatch){
                this.$message.error("请设置url.deleteBatch属性!")
                return
              }
              if (this.selectedRowKeys.length <= 0) {
                this.$message.warning('请选择一条记录！');
                return;
              } else {
                var ids = "";
                for (var a = 0; a < this.selectionRows.length; a++) {
                  ids += this.selectionRows[a].invateuserdetailId + ",";
                }
                var that = this;
                this.$confirm({
                  title: "确认删除",
                  content: "是否删除选中数据?",
                  onOk: function () {
                    deleteAction(that.url.deleteBatch, {ids: ids}).then((res) => {
                      if (res.success) {
                        that.$message.success(res.message);
                        that.loadData();
                        that.onClearSelected();
                      } else {
                        that.$message.warning(res.message);
                      }
                    });
                  }
                });
              }
            },
            handleDelete: function (invateuserdetailId) {
              if(!this.url.delete){
                this.$message.error("请设置url.delete属性!")
                return
              }
              var that = this;
              deleteAction(that.url.delete, {invateuserdetailId: invateuserdetailId}).then((res) => {
                if (res.success) {
                  that.$message.success(res.message);
                  that.loadData();
                } else {
                  that.$message.warning(res.message);
                }
              });
            },

      isNull(data){
        return data === undefined || data === null || data === "" || data === {};
      },
      toFix(value,num1,num2){
        return (parseFloat(value) / Math.pow(10,num1)).toFixed(num2);
      },
         keyupEnter(){
                    document.onkeydown = e =>{
                        let body = document.getElementsByTagName('body')[0]
                        if (e.keyCode === 13  ) {
                           this.loadData(1);
                        }
                    }
                },

        }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less'
</style>